package br.igk.isnmp.exception;

import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * Classe para Excecao
 */
public class BusinessException extends RuntimeException {
	private static final long serialVersionUID = -4162361187869886472L;
	public static int UNKNOW_SEVERITY = 0;
	public static int SYSTEM_SEVERITY = 1;
	public static int RESOURCE_SEVERITY = 2;
	public static int WARNING_SEVERITY = 3;
	public static int USER_SEVERITY = 4;
	protected int severity;
	protected String msg;
	protected String code;
	protected Throwable root;

	/** Cria uma nova excecao do tipo BusinessException */
	public BusinessException(int severity, String code, String msg) {
		this.severity = severity;
		this.code = code;
		this.msg = msg;
		this.root = null;
	}

	/** Cria uma nova excecao do tipo BusinessException */
	public BusinessException(String code, String msg) {
		this.severity = UNKNOW_SEVERITY;
		this.code = code;
		this.msg = msg;
		this.root = null;
	}

	/** Cria uma nova excecao do tipo BusinessException */
	public BusinessException(String msg) {
		this.severity = UNKNOW_SEVERITY;
		this.code = "IGK100";
		this.msg = msg;
		this.root = null;
	}

	/** Cria uma nova excecao do tipo BusinessException */
	public BusinessException() {
		this.severity = UNKNOW_SEVERITY;
		this.code = "IGK100";
		this.msg = "not Avaible";
		this.root = null;
	}

	/** Cria uma nova excecao do tipo BusinessException */
	public BusinessException(int severity, String code, String msg,
			Throwable root) {
		this.severity = severity;
		this.code = code;
		this.msg = msg;
		this.root = root;
	}

	/** Cria uma nova excecao do tipo BusinessException */
	public BusinessException(String code, String msg, Throwable root) {
		this.severity = UNKNOW_SEVERITY;
		this.code = code;
		this.msg = msg;
		this.root = root;
	}

	/** Cria uma nova excecao do tipo BusinessException */
	public BusinessException(String msg, Throwable root) {
		this.severity = UNKNOW_SEVERITY;
		this.code = "IGK100";
		this.msg = msg;
		this.root = root;
	}

	public String getMessage() {
		return "[" + code + "] " + msg;
	}

	public String getShortMessage() {
		return msg;
	}

	public int getSeverity() {
		return this.severity;
	}

	public String getCode() {
		return this.code;
	}

	/** Imprime o stack Trace */
	public void printStackTrace() {
		super.printStackTrace();

		if (root != null) {
			System.out.println("[Root Cause]");
			root.printStackTrace();
		}
	}

	/** Imprime o stack Trace */
	public void printStackTrace(PrintStream s) {
		super.printStackTrace(s);

		if (root != null) {
			s.println("[Root Cause]");
			root.printStackTrace(s);
		}
	}

	/** Imprime o stack Trace */
	public void printStackTrace(PrintWriter w) {
		super.printStackTrace(w);

		if (root != null) {
			w.println("[Root Cause]");
			root.printStackTrace(w);
		}
	}

	/**
	 * Retorna a causa
	 *
	 * @return root cause
	 **/
	public Throwable getRootCause() {
		return this.root;
	}
}
